﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BarTender;
using TSCPrintUnit;

namespace BarcodePrintTool
{
    public partial class BarcodePrintTool : Form
    {
        private static BarTender.Application btApp = new BarTender.Application();
        private static BarTender.Format btFormat = new BarTender.Format();

        public BarcodePrintTool()
        {
            InitializeComponent();
        }

        private void BarcodePrintTool_Load(object sender, EventArgs e)
        {
            PrintLabNum.Text = "1";

            //获取打印机
            foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                InitprinterComboBox.Items.Add(printer);
            }
            InitprinterComboBox.SelectedIndex = 0;
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            string sn_string = BarcodeTextBox.Text;

            InitprinterComboBox.Enabled = false;
            BarcodeTextBox.Enabled = false;
            PrintLabNum.Enabled = false;
            //Dictionary<string, string> mns = new Dictionary<string, string>()
            //    {
            //        { "QRcode128",BarcodeTextBox.Text},
            //        {"ITEM",textBox1.Text},
            //    { "ROHS",textBox2.Text},
            //    { "Producer",textBox3.Text},
            //    { "Version",textBox4.Text},
            //    };
            Dictionary<string, string> mns = new Dictionary<string, string>()
                {
                    { "QRcode128",BarcodeTextBox.Text},                                
                };
            Print("TSC TTP-244 Pro", @"D:\文档1.btw", mns, 1);
            //判断两次SN号是否相等，若不相等则进行生成条形码和打印
            /*if (sn_string != string.Empty)
            {
                try
                {
                    btFormat = btApp.Formats.Open(AppDomain.CurrentDomain.BaseDirectory + "料框.btw", false, "");

                    //向bartender模板传递变量,SN为条形码数据的一个共享名称
                    //btFormat.SetNamedSubStringValue("SN", sn_string);

                    //选择打印机
                    btFormat.Printer = InitprinterComboBox.Text;

                    //设置打印份数
                    int CopiesOfLabel = Int32.Parse(this.PrintLabNum.Text.ToString());
                    btFormat.IdenticalCopiesOfLabel = CopiesOfLabel;

                    //设置打印时是否跳出打印属性
                    btFormat.PrintOut(false, false);

                    //退出时是否保存标签
                    btFormat.Close(BarTender.BtSaveOptions.btSaveChanges);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误信息: " + ex.Message);
                    return;
                }
            }
*/
            InitprinterComboBox.Enabled = true;
            BarcodeTextBox.Enabled = true;
            PrintLabNum.Enabled = true;
        }

        public static void Print(string printerName, string btFileName, Dictionary<string, string> dict, int CopiesOfLabel)
        {
            btFormat = btApp.Formats.Open(btFileName);
            btFormat.PrintSetup.Printer = printerName;
            btFormat.IdenticalCopiesOfLabel = CopiesOfLabel;
            foreach (var item in dict)
            {
                btFormat.SetNamedSubStringValue(item.Key, item.Value);
            }
            btFormat.PrintOut(false, false);
            //不保存标签退出
            btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
        }

        //界面退出时同步退出bartender进程
        private void Form_Stb_Sn_FormClosed(object sender, FormClosedEventArgs e)
        {
            btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TSCPrint tSCPrint=new TSCPrint();
            tSCPrint.AddDict("QRcode128", BarcodeTextBox.Text);
            tSCPrint.Print("TSC TTP-244 Pro",  @"D:\文档1.btw",1);
        }
    }
}
